package com.lmx.web.controller.thirdPart;


import com.lmx.persistence.service.DeviceService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.*;

/*-
 * 床垫处理接口
 */

@Controller
@RequestMapping("/access/darma")
public class BedMatController {
	private static Log logger = LogFactory.getLog(BedMatController.class);

	@Autowired
	private DeviceService deviceService;
	
	@RequestMapping("/warningData")
	@ResponseBody
	public void bedMatWarningData(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("-----------------------------获取床垫预警数据接口-------------------------------------");
		BufferedReader in = null;
		String result = "";
		try {

			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(request.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			//result = "[{\"UUID\":\"9F8978DD1ED5\",\"FirmwareVersion\":null,\"SensorData\":[{\"Data\":[{\"Value\":\"0\",\"At\":\"2016-12-21 16:38:01\"}],\"PropertyCode\":\"2\"}]}]";
			//result ="{\"command\": \"mattressStatus\",\"mattressId\":\"641650000261\",\"type\" : 3 ,\"currentTime\": 1496742832 }";
			//result ="{\"currentTime\":1496742936,\"mattressId\":\"641650000202\",\"command\":\"mattressStatus\",\"type\":6}";
			logger.debug("bedMatWarningData0: " + result);
			if (result != null && !"".equals(result)) {
				deviceService.operateDaermaWarnInfo(result);
			}
			logger.debug("bedMatWarningData: " + result);
			BufferedOutputStream out;
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("yunbao get data ".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug("bedMatWarningData Exception: " + e.toString());
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				logger.debug("bedMatWarningData error "+ex);
			}
		}

		// 数据处理
	}

	/**
	 * 获取本机Ip
	 *
	 * 通过 获取系统所有的networkInterface网络接口 然后遍历 每个网络下的InterfaceAddress组。 获得符合
	 * <code>InetAddress instanceof Inet4Address</code> 条件的一个IpV4地址
	 * 
	 * @return
	 */
	private String localIp() {
		String ip = null;
		Enumeration allNetInterfaces;
		try {
			allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
				List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();
				for (InterfaceAddress add : InterfaceAddress) {
					InetAddress Ip = add.getAddress();
					if (Ip != null && Ip instanceof Inet4Address) {
						ip = Ip.getHostAddress();
					}
				}
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			logger.warn("获取本机Ip失败:异常信息:" + e.getMessage());
		}
		return ip;
	}



}
